#!/usr/bin/env python
import sys, os, os.path
import logging

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

class Dispatcher(webapp.RequestHandler):
    METHOD_BLOG = "blog"
    METHOD_MICROBLOG = "microblog"
    METHOD_STATUS = "status"
    METHOD_DEFAULT = METHOD_MICROBLOG
    
    def get(self, id):
        self.response.headers['Content-Type'] = 'text/html; charset=utf-8'
        
        context = {
            "id" : id
        }
        
        path = os.path.join(os.path.dirname(__file__), "send.htm")
        self.response.out.write(template.render(path, context))
        
    def post(self, id):
        import json
        
        method = self.request.get("method", default_value=self.METHOD_DEFAULT)
        title = self.request.get("title", default_value=None)
        message = self.request.get("message")
        raw_message = self.request.get("raw_message")
        location = self.request.get("location")
        
        logging.info("receive %s from %s: %s", method, location, title)
        
        self.response.headers['Content-Type'] = 'application/json; charset=utf-8'
        self.response.out.write(json.write({
            "method" : method,
            "title" : title,
            "message" : message,
            "location" : location
        }))